﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using VQWEB.administrator.sources;
using VQData;
using System.Data;

namespace VQWEB.administrator.english.excercises
{
    public partial class excercise_group_add : System.Web.UI.Page
    {
        EnglishData EnglishData = new EnglishData();
        eng_excercises_group eng_excercises_group = null;
        int Id = 0;
        protected void Page_Load(object sender, EventArgs e)
        {

            if (RouteData.Values["Id"] != null)
                int.TryParse(RouteData.Values["Id"].ToString(), out Id);
            if (Id > 0)
            {
                frmGroupExcercise.DefaultMode = FormViewMode.Edit;
                eng_excercises_group = EnglishData.GetExcerciseGroupById(Id);
                //news = NewsData.GetNewsById(Id);
                frmGroupExcercise.DataSource = new List<eng_excercises_group> { eng_excercises_group };
                frmGroupExcercise.DataBind();
            }
            else frmGroupExcercise.DefaultMode = FormViewMode.Insert;
        }

        protected void frmGroupExcercise_ItemCreated(object sender, EventArgs e)
        {
            var excercise_control = frmGroupExcercise.Row.FindControl("ltrExcercise") as Literal;
            excercise_control.Text = ShowExcercise(Id);
        }

        private string ShowExcercise(int group_id) {
            string html = string.Empty;
            DataTable table = EnglishData.ViewExcercises();
            if (table != null)
            {
                foreach (DataRow item in table.Rows)
                {
                    bool flag = false;
                    string check = string.Empty;
                    string excercise_title = (string)item["excercise_title"];
                    int excercise_id = (int)item["excercise_id"];
                    string type_code = (item["type_code"] != DBNull.Value) ? (string)item["type_code"] : "null";
                    flag = EnglishData.CheckExcerciseInGroup(excercise_id, group_id);
                    if (flag == true)
                        check = "checked=\"checked\"";
                    html += "<div class=\"checkbox\">";
                    html += "<label>";
                    html += "<input " + check + " name=\"excercise_array_id[]\" type=\"checkbox\" value=" + excercise_id + " />";
                    html += excercise_title + " [" + type_code + "]";
                    html += "</label>";
                    html += "</div>";
                }
            }
            return html;
        
        }
    }
}